<?php

class P2C_Platform {

	var $initial = false;

	var $id;
	var $name;
	var $url;
    var $logo;
    var $active;
    var $projects;
    var $parse_key;
    var $parse_interval;
    var $created_by;
    var $create_date;
    var $parse_date;
    var $update_date;
    var $country;
    var $proxy;

	public static function find( $args = '' ) {
        global $wpdb;
        
		$defaults = array(
			'post_status' => 'any',
			'posts_per_page' => -1,
			'offset' => 0,
			'orderby' => 'id',
			'order' => 'ASC' );

		$args = wp_parse_args( $args, $defaults );
        $order_by = $args['orderby'] . ' ' . $args['order'];

		$platforms = $wpdb->get_results($wpdb->prepare("SELECT pl.id,pl.name,pl.url,pl.logo,pl.active,pl.parse_key,pl.parse_interval,pl.create_date,pl.created_by,pl.parse_date,pl.update_date,count(pr.id) as projects, pl.country, pl.proxy 
            FROM p2c_platform AS pl LEFT JOIN p2c_project AS pr ON pl.id = pr.platform_id 
            GROUP BY pl.id,pl.name,pl.url,pl.logo,pl.active,pl.parse_key,pl.parse_interval,pl.create_date,pl.created_by,pl.parse_date,pl.update_date, pl.country, pl.proxy
            ORDER BY $order_by 
            LIMIT %d,%d;",$args['offset'], $args['posts_per_page']));

		$objs = array();

		foreach ( $platforms as $platform )
			$objs[] = new self( $platform );

		return $objs;
	}
    
    public static function rowcount( ) {
        global $wpdb;

		$platforms = $wpdb->get_var("SELECT COUNT(*) FROM p2c_platform;");
        
		return $platforms;
	}

	public function __construct( $platform = null ) {
        global $wpdb, $current_user;
        get_currentuserinfo();
        
		$this->initial = true;
        $this->projects = 0;
        $this->created_by = $current_user->user_login;
        $this->create_date = date('Y-m-d H:i:s');
        $this->update_date = date('Y-m-d H:i:s');
                
        if($platform && !is_object($platform)) {
            $platform = $wpdb->get_row($wpdb->prepare("SELECT * FROM p2c_platform WHERE id = %d", $platform));
        }
        
		if ( $platform ) {
			$this->initial = false;
			$this->id = $platform->id;
			$this->name = $platform->name;
			$this->url = $platform->url;
			$this->logo = $platform->logo;
            $this->active = $platform->active;
            $this->projects = $platform->projects;
            $this->parse_key=$platform->parse_key;
            $this->parse_interval=$platform->parse_interval;
            $this->created_by=$platform->created_by;
            $this->create_date=$platform->create_date;
            $this->parse_date=$platform->parse_date;
            $this->update_date=$platform->update_date;
            $this->country=$platform->country;
            $this->proxy=$platform->proxy;
		}
    }
    
	/* Save */

	function save() {
        global $wpdb; 
        
		if ( $this->initial ) {
			$wpdb->insert("p2c_platform", array(
               "name" => $this->name,
               "logo" => $this->logo,
               "url" => $this->url,
               "active" => $this->active,
               "parse_key"=>$this->parse_key,
                "parse_interval"=>$this->parse_interval,
                "created_by"=>$this->created_by,
                "create_date"=>$this->create_date,
                "parse_date"=>$this->parse_date,
                "update_date"=>$this->update_date,
                "country"=>$this->country,
                "proxy"=>$this->proxy
            ));
            
            $this->initial = false;
            $this->id = $wpdb->insert_id;
		} else {
			$wpdb->update( 
                'p2c_platform', 
                array( 
                   "name" => $this->name,
                   "logo" => $this->logo,
                   "url" => $this->url,
                   "active" => $this->active,
                   "parse_key"=>$this->parse_key,
                    "parse_interval"=>$this->parse_interval,
                    "parse_date"=>$this->parse_date,
                    "update_date"=>date('Y-m-d H:i:s'),
                    "country"=>$this->country,
                    "proxy"=>$this->proxy
                ), 
                array( 'id' => $this->id )
            );
		}

		return $this->id;
	}
}


?>